<?php

class Mageex_Robot_Block_Adminhtml_Robot_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form
{
	protected $_storeModel;
    protected $_attributes;
    protected $_addMapButtonHtml;
    protected $_removeMapButtonHtml;
    protected $_shortDateFormat;
	protected $_strSerialized;
	
	public function __construct()
    {
        parent::__construct();
        $this->setTemplate('robot/wizard.phtml');
		$this->_strSerialized = Mage::registry('robot_data')->getField();
    }
	public function getTypeCampaign()
	{
		if( Mage::registry('robot_data') && Mage::registry('robot_data')->getId() ) {
			$type = Mage::registry('robot_data')->getType();
		}else{
			$type = Mage::registry('robot_type');
		}
		return $type;
	}
	public function getNumberOfRecords()
	{
		if( Mage::registry('robot_data') && Mage::registry('robot_data')->getId() ) {
			$number = Mage::registry('robot_data')->getNumberOfRecords();
		}else{
			$number = 1;
		}
		return $number;
	}
  public function getAttributes($entityType)
    {
        if (!isset($this->_attributes[$entityType])) {
            switch ($entityType) {
                case 'product':
                    $attributes = Mage::getSingleton('catalog/convert_parser_product')
                        ->getExternalAttributes();
                    break;

                case 'customer':
                    $attributes = Mage::getSingleton('customer/convert_parser_customer')
                        ->getExternalAttributes();
                    break;
            }

            array_splice($attributes, 0, 0, array(''=>$this->__('Choose an attribute')));
            $this->_attributes[$entityType] = $attributes;
        }
        return $this->_attributes[$entityType];
    }

    public function getValue($key, $default='', $defaultNew = null)
    {
        if (null !== $defaultNew) {
            if (0 == $this->getProfileId()) {
                $default = $defaultNew;
            }
        }

        $maps = Mage::helper('robot/func')->getFieldMappingFromStr($this->_strSerialized, $key);
		
        return $this->htmlEscape(strlen($maps) > 0 ? $maps : $default);
    }

    public function getSelected($key, $value)
    {
		if( Mage::registry('robot_data') && Mage::registry('robot_data')->getId() ) {
			$storeId = Mage::registry('robot_data')->getStoreId();
		}else{
			$storeId = 0;
		}
        return $storeId==$value ? 'selected="selected"' : '';
    }
	public function getFieldSelected($key, $value)
	{
		$maps = Mage::helper('robot/func')->getFieldMappingFromStr($this->_strSerialized, $key);
		if ($maps == $value) {
			return "selected='selected'";
		}
		
	}
    public function getChecked($key)
    {
        return $this->getData($key) ? 'checked="checked"' : '';
    }

    public function getMappings($entityType)
    {
        $key = 'map/'.$entityType.'/db';
		
		if( Mage::registry('robot_data') && Mage::registry('robot_data')->getId() ) {
			
			$maps = Mage::helper('robot/func')->getFieldMappingFromStr($this->_strSerialized, $key);
			
			$maps = Mage::helper('robot/func')->removeValueFromArr(0, $maps);
			return $maps;
		}else{
			return array();
		}
        
    }
	
    public function getAddMapButtonHtml()
    {
        if (!$this->_addMapButtonHtml) {
            $this->_addMapButtonHtml = $this->getLayout()->createBlock('adminhtml/widget_button')->setType('button')
                ->setClass('add')->setLabel($this->__('Add Field Mapping'))
                ->setOnClick("addFieldMapping()")->toHtml();
        }
        return $this->_addMapButtonHtml;
    }

    public function getRemoveMapButtonHtml()
    {
        if (!$this->_removeMapButtonHtml) {
            $this->_removeMapButtonHtml = $this->getLayout()->createBlock('adminhtml/widget_button')->setType('button')
                ->setClass('delete')->setLabel($this->__('Remove'))
                ->setOnClick("removeFieldMapping(this)")->toHtml();
        }
        return $this->_removeMapButtonHtml;
    }

    public function getProductTypeFilterOptions()
    {
        $options = Mage::getSingleton('catalog/product_type')->getOptionArray();
        array_splice($options, 0, 0, array(''=>$this->__('Any Type')));
        return $options;
    }

    public function getProductAttributeSetFilterOptions()
    {
        $options = Mage::getResourceModel('eav/entity_attribute_set_collection')
            ->setEntityTypeFilter(Mage::getModel('catalog/product')->getResource()->getTypeId())
            ->load()
            ->toOptionHash();

        $opt = array();
        $opt = array(''=>$this->__('Any Attribute Set'));
        if ($options) foreach($options as $index => $value) {
            $opt[$index]  = $value;
        }
        //array_slice($options, 0, 0, array(''=>$this->__('Any Attribute Set')));
        return $opt;
    }

    public function getProductVisibilityFilterOptions()
    {
        $options = Mage::getSingleton('catalog/product_visibility')->getOptionArray();

        array_splice($options, 0, 0, array(''=>$this->__('Any Visibility')));
        return $options;
    }

    public function getProductStatusFilterOptions()
    {
        $options = Mage::getSingleton('catalog/product_status')->getOptionArray();

        array_splice($options, 0, 0, array(''=>$this->__('Any Status')));
        return $options;
    }

    public function getStoreFilterOptions()
    {
        if (!$this->_filterStores) {
            #$this->_filterStores = array(''=>$this->__('Any Store'));
            $this->_filterStores = array();
            foreach (Mage::getConfig()->getNode('stores')->children() as $storeNode) {
                if ($storeNode->getName()==='default') {
                    //continue;
                }
                $this->_filterStores[$storeNode->getName()] = (string)$storeNode->system->store->name;
            }
        }
        return $this->_filterStores;
    }

    /**
     * Retrieve system store model
     *
     * @return Mage_Adminhtml_Model_System_Store
     */
    protected function _getStoreModel() {
        if (is_null($this->_storeModel)) {
            $this->_storeModel = Mage::getSingleton('adminhtml/system_store');
        }
        return $this->_storeModel;
    }

    public function getWebsiteCollection()
    {
        return $this->_getStoreModel()->getWebsiteCollection();
    }

    public function getGroupCollection()
    {
        return $this->_getStoreModel()->getGroupCollection();
    }

    public function getStoreCollection()
    {
        return $this->_getStoreModel()->getStoreCollection();
    }
	public function getUrlPreview()
	{
		return $this->getUrl('robot/adminhtml_robot/preview');
	}
}